function [ universal_individuals ] = universal( individuals, n_of_selection )
% UNIVERSAL
%   Esta funcion realiza el metodo de seleccion universal a partir de los
%   individuos recibidos como parametro y la cantidad de individuos a
%   seleccionar.

fitness_array = zeros(1, length(individuals));
f_array = zeros(1, length(individuals));
for i = 1:length(individuals)
   f_array(i) = fitness(individuals{i});
end
for i = 1:length(individuals)
   fitness_array(i) = f_array(i) + 2*abs(min(f_array));
end

total_fitness = sum(fitness_array);

relative_fitness = zeros(1, length(individuals));
accumulative_fitness = zeros(1, length(individuals));
for i = 1:length(individuals)
    relative_fitness(i) = fitness_array(i) / total_fitness;
    if i > 1
        accumulative_fitness(i) = accumulative_fitness(i-1) + relative_fitness(i);
    else
        accumulative_fitness(i) = relative_fitness(i);
    end
end

universal_individuals = cell(1, n_of_selection);
n_rand = rand();

for i = 1:n_of_selection
    j = 1;
    r = (n_rand + i - 1) / n_of_selection;
    while r > accumulative_fitness(j)
        j = j + 1;
    end
    universal_individuals{i} = individuals{j};
end

end
